package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.*;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private LabelInfoService labelInfoService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private FacilityInfoService facilityInfoService;
    @Override
    public ApartmentItemVo getApartmentItemVoById(Long apartmentId) {
        ApartmentInfo apartmentInfo = this.getById(apartmentId);
        List<RoomInfo> roomList = roomInfoMapper.selectList(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId, apartmentId));
        // 公寓中的标签和图片以及该公寓下房间最低租金
        List<LabelInfo> apartmentLabelInfoList = new ArrayList<>();
        List<ApartmentLabel> apartmentLabelList = apartmentLabelService.list(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId, apartmentId));
        for (ApartmentLabel apartmentLabel : apartmentLabelList) {
            apartmentLabelInfoList.add(labelInfoService.getById(apartmentLabel.getLabelId()));
        }

        List<GraphInfo> apartmentGraphVoList = graphInfoService.list(new LambdaQueryWrapper<GraphInfo>().eq(GraphInfo::getItemId, apartmentId).eq(GraphInfo::getItemType, ItemType.APARTMENT));

        // 查找该公寓下房间最低租金
        BigDecimal minRent = roomList.stream().map(RoomInfo::getRent).min(BigDecimal::compareTo).get();

        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();

        BeanUtils.copyProperties(apartmentInfo, apartmentItemVo);
        apartmentItemVo.setLabelInfoList(apartmentLabelInfoList);
        apartmentItemVo.setGraphVoList(apartmentGraphVoList);
        apartmentItemVo.setMinRent(minRent);
        return apartmentItemVo;
    }

    @Override
    public ApartmentDetailVo getApartmentDetailById(Long id) {
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        ApartmentInfo apartmentInfo = this.getById(id);
        if (apartmentInfo != null) {
            BeanUtils.copyProperties(apartmentInfo, apartmentDetailVo);

            List<GraphVo> graphVoList = graphInfoService.getGraphVoListByIdAndItemType(id, ItemType.APARTMENT);
            apartmentDetailVo.setGraphVoList(graphVoList);

            List<LabelInfo> labelInfoList =  new ArrayList<>();
            List<ApartmentLabel> apartmentLabelList = apartmentLabelService.list(new LambdaQueryWrapper<ApartmentLabel>().eq(ApartmentLabel::getApartmentId, id));
            for (ApartmentLabel apartmentLabel : apartmentLabelList) {
                labelInfoList.add(labelInfoService.getById(apartmentLabel.getLabelId()));
            }
            apartmentDetailVo.setLabelInfoList(labelInfoList);

            List<FacilityInfo> facilityInfoList = facilityInfoService.getApartmentFacilityInfoListById(id);
            apartmentDetailVo.setFacilityInfoList(facilityInfoList);

            BigDecimal minRent = null;
            List<RoomInfo> roomInfos = roomInfoMapper.selectList(new LambdaQueryWrapper<RoomInfo>().eq(RoomInfo::getApartmentId, id));
            if (roomInfos != null && !roomInfos.isEmpty()) {
                minRent = roomInfos.stream().map(RoomInfo::getRent).min(BigDecimal::compareTo).get();
            }
            apartmentDetailVo.setMinRent(minRent);

            Boolean isDelete = apartmentInfo.getIsDeleted()==1;
            apartmentDetailVo.setIsDelete(isDelete);
        }
        return apartmentDetailVo;
    }
}




